package demo.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

@Controller
public class OAuth2LoginViewController {

	final Logger logger = LoggerFactory.getLogger(getClass());
		
	@RequestMapping("/login")
	public String getLogin(Map<String, Object> model, HttpServletRequest request) throws Exception {

		if (request.getAttribute("_csrf") != null) {
			model.put("_csrf", request.getAttribute("_csrf"));
		}
	
		AuthenticationException authExp = (AuthenticationException) request.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
		//login error message 
//		Object error = request.getAttribute("error");		
//		if(error != null) {			
//			model.put("error", error);			
//		}
		if(authExp != null) {
			model.put("error", HtmlUtils.htmlEscape(authExp.getMessage()));		
		}
		return "customized_login";
	}
}
